package fr.mywiki.model.ejbAdapter;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import fr.mywiki.model.ejbAdapter.entity.AccountLocalHome;
import fr.mywiki.model.ejbAdapter.entity.EntryLocalHome;
import fr.mywiki.model.ejbAdapter.entity.StructureLocalHome;
import fr.mywiki.model.ejbAdapter.entity.VersionLocalHome;

/**
 * <p>
 * This class centralizes all access to local home interfaces.
 * </p>
 * <p>
 * It works as a singleton.
 * </p>
 * 
 * @author tlombard
 */
public class EntityHomeCache {
	/** The jndi context. */
	private Context ctx = null;

	/////////////////////////////////////////////////////////////////////
	//                 The entity local homes                          //
	/////////////////////////////////////////////////////////////////////	
	
	/** The member entity home. */
	private AccountLocalHome accountHome = null;
	/** The entry entity home. */
	private EntryLocalHome entryHome = null;
	/** The structure entity home. */
	private StructureLocalHome structureHome = null;
	/** The version entity home. */
	private VersionLocalHome versionHome = null;
	
	/////////////////////////////////////////////////////////////////////
	//                 The singleton management                        //
	/////////////////////////////////////////////////////////////////////
	
	/** The instance of the class (in order not to take to much place) */
	private static EntityHomeCache instance = null;
	
	/** Private constructor. */
	private EntityHomeCache() {
		try {
			ctx = new InitialContext();
			accountHome = (AccountLocalHome) PortableRemoteObject.narrow(
					ctx.lookup(AccountLocalHome.JNDI_NAME), AccountLocalHome.class);
			entryHome = (EntryLocalHome) PortableRemoteObject.narrow(
					ctx.lookup(EntryLocalHome.JNDI_NAME), EntryLocalHome.class);
			structureHome = (StructureLocalHome) PortableRemoteObject.narrow(
					ctx.lookup(StructureLocalHome.JNDI_NAME), StructureLocalHome.class);
			versionHome = (VersionLocalHome) PortableRemoteObject.narrow(
					ctx.lookup(VersionLocalHome.JNDI_NAME), VersionLocalHome.class);
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	/** Returns the unique instance of the class. */
	public static synchronized EntityHomeCache getInstance() {
		if (instance == null)
			instance = new EntityHomeCache();
		return instance;
	}

	/////////////////////////////////////////////////////////////////////
	//                 Getters for the entity homes                   //
	/////////////////////////////////////////////////////////////////////
	
	public AccountLocalHome getAccountHome() {
		return accountHome;
	}
	
	public EntryLocalHome getEntryHome() {
		return entryHome;
	}
	
	public StructureLocalHome getStructureHome() {
		return structureHome;
	}
	
	public VersionLocalHome getVersionHome() {
		return versionHome;
	}
}
